import { Module } from 'vuex'
import { IMainState } from './type'
import { IRootState } from '../types'
import {
  getSongsList,
  getPlaylist,
  getLikeSong,
  getNewSong,
  getSongsById,
  getSongInfo,
  getSongText
} from '@/service/main/main'
import { IAccount, IPhone } from '@/service/login/types'
import localCache from '@/utils/cache'
import router from '../../router/index'

const ID = localCache.getCache('ID')

const loginModule: Module<IMainState, IRootState> = {
  namespaced: true,
  state() {
    return {
      userPlayList: [],
      newPlayList: [],
      singUrl: '',
      singListInfo: [],
      singListIDInfo: [],
      playList: null
    }
  },
  mutations: {
    setSongList(state, songList) {
      state.userPlayList = songList
    },
    setNewSongList(state, newSongInfo) {
      state.newPlayList = newSongInfo
    },
    setSingUrl(state, songUrlList) {
      state.singUrl = songUrlList
    },
    setSingListInfo(state, { singListInfo, singListIDInfo, playList }) {
      state.singListInfo = singListInfo
      state.singListIDInfo = singListIDInfo
      state.playList = playList
    }
  },
  actions: {
    async getPlayListAction({ commit }) {
      const playListRes = await getLikeSong(ID)
      const randomS = Math.floor(Math.random() * 100)
      const randomE = Number(randomS + 8)
      const listArr = playListRes.ids.slice(randomS, randomE)
      const { songs: songListInfo } = await getSongInfo(listArr)
      // const test = await getSongText(listArr)
      const songInfo: {
        songName: any
        singer: any
        singID: any
        singAl: any
        time: any
        mv: any
      }[] = []
      songListInfo.map((item: any) => {
        return songInfo.push({
          songName: item.name,
          singer: item.ar[0].name,
          singID: item.id,
          singAl: item.al,
          time: item.publishTime,
          mv: item.mv
        })
      })
      commit('setSongList', songInfo)
    },
    async getSingUrlAction({ commit }, singID) {
      const { data: songUrlList } = await getSongsById(singID)

      commit('setSingUrl', songUrlList[0].url)
    },
    async getSingListAction({ commit }) {
      const singListIDInfo: any[] = []
      const singListInfo: { singListName: any; singListImg: any }[] = []
      const { playlist: singList } = await getPlaylist(ID)
      singList?.map((item: { name: any; coverImgUrl: any; id: any }) => {
        return singListInfo.push({
          singListName: item.name,
          singListImg: item.coverImgUrl
        })
      })
      singList?.map((item: { id: any }) => {
        return singListIDInfo.push(item.id)
      })
      const randomList = Math.floor(Math.random() * 6 + 1)
      const { playlist: singUrlList } = await getSongsList(singListIDInfo[randomList])
      const { playlist: singUrlList2 } = await getSongsList(singListIDInfo[randomList + 1])
      const playListID: any = []
      const playListID2: any = []
      singUrlList.tracks.map((item: { name: any; ar: { name: any }[]; id: any }) => {
        return playListID.push({
          songName: item.name,
          singer: item.ar[0].name,
          singID: item.id
        })
      })
      singUrlList2.tracks.map((item: { name: any; ar: { name: any }[]; id: any }) => {
        return playListID2.push({
          songName: item.name,
          singer: item.ar[0].name,
          singID: item.id
        })
      })

      const playList = {
        listInfo: playListID ?? '',
        listName: (singUrlList.name ??= ''),
        listInfo2: playListID2,
        listName2: (singUrlList2.name ??= '')
      }

      commit('setSingListInfo', { singListInfo, singListIDInfo, playList })
    },
    async getNewSingAction({ commit }) {
      const randomS = Math.floor(Math.random() * 25)
      const randomE = Number(randomS + 8)
      const { data: testdata } = await getNewSong()
      const newSongList = testdata.dailySongs.slice(randomS, randomE)
      const newSongInfo: { songName?: any; singer?: any; singID?: any }[] = []
      newSongList.map((item: any) => {
        return newSongInfo.push({
          songName: item.name,
          singer: item.ar[0].name,
          singID: item.id
        })
      })
      commit('setNewSongList', newSongInfo)
      // commit('setSingUrl', newSongList)
      //
    }
  },
  getters: {}
}
export default loginModule
